package com.aaa.three.controller;

import com.aaa.three.pojo.domain.PersonalQuery;
import com.aaa.three.pojo.domain.UserEmp;
import com.aaa.three.pojo.query.PersonalQueryy;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.PersonalInfoService;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Author:lyq
 * @Date:2022/2/2221:25
 */
@Controller
@RequestMapping("usersource")
public class UserSourceController {
    @Autowired
    private PersonalInfoService personalInfoService;

    @RequestMapping("upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile hehe) throws Exception{

        ExcelReaderBuilder read = EasyExcel.read(  hehe.getInputStream(), UserEmp.class ,null );
        List<UserEmp> objects = read.doReadAllSync();

        for (int i=2;i<objects.size();i++) {

        }

        return  AjaxResult.success();

    }

    @RequestMapping("download")
    public void download(PersonalQueryy personalQueryy, HttpServletResponse response) throws Exception{
        AjaxResult ajaxResult=personalInfoService.query(personalQueryy);
        Map map = (Map) ajaxResult.getData();
        List<PersonalQuery> currentData = (List<PersonalQuery>) map.get("currentData");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(   response.getOutputStream() , PersonalQuery.class     )  .sheet("用户信息").doWrite( currentData      );

    }
}
